const shareUtil = require('../../../utils/shareUtil.js');
const app=getApp();
let  imgUrl=app.globalData.imgUrl;
var t = [ [ "00", "01", "02", "03" ], [ "10", "11", "12", "13" ], [ "20", "21", "22", "23" ], [ "30", "31", "32", "33" ] ], n = 75, a =imgUrl+ "/pintu/pic01.jpg";

Page({
    data: {
        isWin: false
    },
    shuffle: function() {
        t = [ [ "00", "01", "02", "03" ], [ "10", "11", "12", "13" ], [ "20", "21", "22", "23" ], [ "30", "31", "32", "33" ] ];
        for (var n = 3, a = 3, i = 0; i < 160; i++) {
            var e = Math.round(4 * Math.random());
            0 == e && 0 != n && (t[n][a] = t[n - 1][a], t[n - 1][a] = "33", n -= 1), 1 == e && 3 != n && (t[n][a] = t[n + 1][a], 
            t[n + 1][a] = "33", n += 1), 3 == e && 0 != a && (t[n][a] = t[n][a - 1], t[n][a - 1] = "33", 
            a -= 1), 4 == e && 3 != a && (t[n][a] = t[n][a + 1], t[n][a + 1] = "33", a += 1);
        }
    },
    drawCanvas: function() {
        var i = this.ctx;
        i.clearRect(0, 0, 300, 300);
        for (var e = 0; e < 4; e++) for (var o = 0; o < 4; o++) if ("33" != t[e][o]) {
            var r = parseInt(t[e][o] / 10), s = t[e][o] % 10;
            i.drawImage(a, s * n, r * n, n, n, o * n, e * n, n, n);
        }
        i.draw();
    },
    touchBox: function(i) {
        if (!this.data.isWin) {
            var e = i.changedTouches[0].x, o = i.changedTouches[0].y, r = parseInt(o / n), s = parseInt(e / n);
            if ("33" != t[r][s] && (this.moveBox(r, s), this.drawCanvas(), this.isWin())) {
                var f = this.ctx;
                f.drawImage(a, 0, 0), f.setFillStyle("#e64340"), f.setTextAlign("center"), f.setFontSize(60), 
                f.fillText("游戏成功", 150, 150), f.draw();
            }
        }
    },
    moveBox: function(n, a) {
        return n > 0 && "33" == t[n - 1][a] ? (t[n - 1][a] = t[n][a], void (t[n][a] = "33")) : n < 3 && "33" == t[n + 1][a] ? (t[n + 1][a] = t[n][a], 
        void (t[n][a] = "33")) : a > 0 && "33" == t[n][a - 1] ? (t[n][a - 1] = t[n][a], 
        void (t[n][a] = "33")) : a < 3 && "33" == t[n][a + 1] ? (t[n][a + 1] = t[n][a], 
        void (t[n][a] = "33")) : void 0;
    },
    isWin: function() {
        for (var n = 0; n < 4; n++) for (var a = 0; a < 4; a++) if (t[n][a] != 10 * n + a) return false;
        return this.setData({
            isWin: true
        }), true;
    },
    restartGame: function() {
        this.setData({
            isWin: false
        }), this.shuffle(), this.drawCanvas();
    },
    onLoad: function(t) {
        let that=this;
        wx.downloadFile({
          url: imgUrl+"/pintu/" + t.level, 
          success (res) {
            if (res.statusCode === 200) {
              that.setData({
                url: res.tempFilePath,
              })
              a= res.tempFilePath
              that.ctx = wx.createCanvasContext("myCanvas")
              that.shuffle()
              that.drawCanvas();
            }
          }
        })
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage:shareUtil.shareConfig,
    onShareTimeline:shareUtil.shareConfig,
});